﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;

namespace VCN.admin
{
    public partial class AddProduct : System.Web.UI.Page
    {   
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                this.LoadData();
                Page.Title = "Thêm sản phẩm";
            }
        }
        private void LoadData()
        {
            CategoriesDAO v_CategoryDAO = new CategoriesDAO();
            DataTable v_dt = v_CategoryDAO.selectAll2Dt();
            ddl_Category.DataSource = v_dt;
            ddl_Category.DataTextField = "CATEGORY_NAME";
            ddl_Category.DataValueField = "Id";
            ddl_Category.DataBind();
        }

        protected void btnAdd_click(object sender, EventArgs e)
        {
            if (FileUpload.HasFile)
            {
                VCN.App.DataTransferObjects.Product v_Product = new VCN.App.DataTransferObjects.Product();
                v_Product.Name = txt_ProductName.Text;
                v_Product.Description = txt_ProductDescription.Text;
                v_Product.CateId = int.Parse(ddl_Category.SelectedItem.Value);
                v_Product.Image = this.doUploadPImage();
                v_Product.ProductKey = txt_ProductKey.Text;
                ProductDAO v_ProductDAO = new ProductDAO();
                v_ProductDAO.insert(v_Product);
                Response.Redirect("ProductList.aspx");
            }
            else lbl_Error.Visible = true;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txt_ProductDescription.Text = "";
            txt_ProductName.Text = "";
            txt_ProductKey.Text = "";
        }

        protected void btnCancel_click(object sender, EventArgs e)
        {
            Response.Redirect("ProductList.aspx");
        }
        private string doUploadPImage()
        {
            if (FileUpload.HasFile)
            {
                var v_str_fstorage = string.Format("../Images/Product/{0}", FileUpload.FileName);
                this.FileUpload.SaveAs(Server.MapPath(v_str_fstorage));
            }
            return FileUpload.FileName;
        }
    }
}
